<!--
 * @Date: 2020-03-30 16:00:40
 * @LastEditors: peng qiao
 * @LastEditTime: 2020-03-30 17:23:43
 -->
<template>
  <div class="house-list">
    <van-nav-bar title="选择楼栋房号" left-text="取消" @click-left="$router.go(-1)" fixed />
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <van-cell v-for="item in houseList" :key="item.houseCode" :title="item.houseCode" @click="selectHouse(item)"/>
      </van-list>
    </van-pull-refresh>
    <van-popup v-model="showLoading">
      <van-loading color="#DC4630" />
    </van-popup>
  </div>
</template>

<script>
  import houseService from "@/controller/myhouse.js";
  import constant from "@/controller/constant.js";
  import bus from "@/utils/eventBus"
  export default {
    data() {
      return {
        loading: false,
        finished: false,
        isLoading: false,
        curPage: 1,
        pageSize: 50,
        showLoading: false,
        houseList: [],
        id: ''
      }
    },
    mounted() {
      this.id = this.$route.query.id
    },
    methods: {
      onRefresh() {
        this.houseList = [];
        // this.finished = false
        // setTimeout(() => {
        //   this.curPage = 1;
        //   this.pageSize = 10;
        //   this.getMyExpress(this.id);
        //   this.isLoading = false;
        // }, 500);
        this.curPage = 1;
        this.pageSize = 50;
        this.onLoad()
      },
      selectHouse(item) {
        bus.$emit('selectHouse',item)
        this.$router.go(-1)
      },
      getMyExpress(id) {
        houseService
          .community_house_neighborhood_page(id,this.pageSize, this.curPage)
          .then(res => {
            if (res.code === "0") {
              if (
                (res.datas && res.datas.records.length === 0) ||
                res.datas === null
              ) {
                this.$toast("暂无更多数据");
                this.loading = false;
                this.isLoading = false;
                this.finished = true;
                return;
              }
              this.$nextTick(() => {
                this.loading = false;
                this.isLoading = false;
              });
              this.houseList = [...this.houseList, ...res.datas.records];
              this.curPage = this.curPage + 1;
              if (res.datas.records.length < this.pageSize) {
                this.finished = true;
              }
            } else {
              if (res && res.msg) {
                this.$toast(res.msg);
              } else {
                this.$toast(constant.unusualMsg);
              }
            }
          });
      },
      onLoad() {
        this.getMyExpress(this.id);
      }
    }
  }
</script>

<style>

</style>